package com.qianfeng.smartdevices.controller;


import com.github.pagehelper.PageInfo;
import com.qianfeng.smartdevices.annotations.AopLogAnnotation;
import com.qianfeng.smartdevices.dto.R;
import com.qianfeng.smartdevices.pojo.User;
import com.qianfeng.smartdevices.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/users")
public class UserController {

    private UserService userService;
    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @PostMapping("/user")
    @AopLogAnnotation("用户添加")
    public R addUser(@RequestBody User user) throws Exception {
        userService.addUser(user);
        return R.setOK();
    }

    @GetMapping("/users")
    @AopLogAnnotation("分页查询用户")
    public R findAllUsers(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, String username,String status) {
        PageInfo<User> pageInfo = userService.findAllUsers(page, limit,username,status);
        return R.setOK(pageInfo);
    }

    @PutMapping("/user")
    @AopLogAnnotation("用户查询")
    public R updateUser(@RequestBody User user) {
        int result=  userService.updateUser(user);
        return R.setOK();
    }

    @DeleteMapping("/users")
    @AopLogAnnotation("用户删除")
    public R deleteUsersByIds(Long[] ids) {
        userService.deleteUsersByIds(ids);
        return R.setOK();
    }

}
